using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;

public partial class admin_shipment_cost_add : SiteAdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;
    }
    protected void Create_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate();
            if (Page.IsValid)
            {
                double weightRangeFrom;
                double weightRangeTo;
                double shipmentCostGround;
                double shipmentCostStandard;
                double shipmentCostExpress;
                double shipmentCostOvernight;
                string weightUnit = "KG";
                bool isActive;
                bool isPercentage;

                double.TryParse(tbxWeightRangeFrom.Text.Trim(), out weightRangeFrom);
                double.TryParse(tbxWeightRangeTo.Text.Trim(), out weightRangeTo);

                if (!ShipmentCost.IsWeightRangeValid(weightRangeFrom, weightUnit))
                {
                    uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostDuplicateWeightRangeFrom);
                    uccResponseMessage.Visible = true;
                }
                else if (!ShipmentCost.IsWeightRangeValid(weightRangeTo, weightUnit))
                {
                    uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostDuplicateWeightRangeTo);
                    uccResponseMessage.Visible = true;
                }
                else
                {
                    double.TryParse(tbxGroundShippingCost.Text.Trim(), out shipmentCostGround);
                    double.TryParse(tbxStandardShippingCost.Text.Trim(), out shipmentCostStandard);
                    double.TryParse(tbxExpressShippingCost.Text.Trim(), out shipmentCostExpress);
                    double.TryParse(tbxOvernightShippingCost.Text.Trim(), out shipmentCostOvernight);

                    isActive = cbxIsActive.Checked;
                    isPercentage = cbxPercentage.Checked;
                    ShipmentCost shipmentCost = new ShipmentCost(0, weightRangeFrom, weightRangeTo, weightUnit, shipmentCostGround, shipmentCostStandard, shipmentCostExpress, shipmentCostOvernight, isActive, false, DateTime.Now,isPercentage);
                    shipmentCost.Create();
                    uccResponseMessage.Visible = true;
                    uccResponseMessage.SetSuccessMessage(AutoGearCommonMessage.ShipmentCostCreateSuccessful);
                    ClearControls();
                }
            }
        }
        catch (Exception ex)
        {
            uccResponseMessage.SetFailMessage(AutoGearCommonMessage.ShipmentCostCreateFailed);
            uccResponseMessage.Visible = true;
        }
    }

    private void ClearControls()
    {
        tbxWeightRangeFrom.Text = string.Empty;
        tbxWeightRangeTo.Text = string.Empty;
        tbxStandardShippingCost.Text = string.Empty;
        tbxExpressShippingCost.Text = string.Empty;
        tbxOvernightShippingCost.Text = string.Empty;
        cbxIsActive.Checked = false;
        cbxPercentage.Checked = false;
    }
}



